﻿using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using Microsoft.Practices.ServiceLocation;
using Pap.Core.Bootstrappers;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web
{
    /// <summary>
    /// 
    /// </summary>
    public class Global : System.Web.HttpApplication
    {
        #region [ Session ]

        /// <summary>
        /// Gets or sets the current language.
        /// </summary>
        /// <value>
        /// The current language.
        /// </value>
        protected string CurrentLanguage
        {
            get { return Session["Language"] != null ? Session["Language"].ToString() : null; }
            set
            {
                Session["Language"] = value;
                var cultureInfo = new System.Globalization.CultureInfo(value);
                cultureInfo.DateTimeFormat = new CultureInfo("en-US", false).DateTimeFormat;
                Thread.CurrentThread.CurrentCulture = cultureInfo;
                Thread.CurrentThread.CurrentUICulture = cultureInfo;
            }
        }

        #endregion

        ///<summary>
        ///</summary>
        public static HttpApplication ApplicationInstance
        {
            private set; get;
        }

        /// <summary>
        /// Handles the Start event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Application_Start(object sender, EventArgs e)
        {
            ApplicationInstance = this;
            Bootstrapper.Run();
        }

        /// <summary>
        /// Handles the Start event of the Session control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Session_Start(object sender, EventArgs e)
        {
            if (HttpContext.Current.User != null)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                        IMembershipRepository repo = ServiceLocator.Current.GetInstance<IMembershipRepository>();
                        IUser user = repo.FindUsers(u => u.Username.ToLower() == id.Name.ToLower()).First();
                        RegistryInfo.UserID = user.UserID;
                        UIPermissions.SetPermissions(user.Permissions.Select(perm => perm.PermissionName).ToArray());
                    }
                }
            }
        }

        /// <summary>
        /// Handles the BeginRequest event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Handles the AuthenticateRequest event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the Error event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Application_Error(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the End event of the Session control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Session_End(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the End event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Application_End(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the PreRequestHandlerExecute event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session != null)
            {
                if (CurrentLanguage == null)
                {
                    CurrentLanguage = "ar-EG";
                }
                else CurrentLanguage = CurrentLanguage;
                Page page = HttpContext.Current.Handler as Page;
                if (page != null)
                {
                    if (CurrentLanguage == "en-US")
                    {
                        page.Theme = "DefaultEn";
                    }
                    else
                    {
                        page.Theme = "DefaultAr";
                    }
                }
            }
        }
    }
}